
import React from 'react';

import styles from './ClearPoints.css';
import {Alert, Button, message, Popconfirm} from 'antd';
import RuleService from "../../business/rule/RuleService";
import UserService from "../../business/user/UserService";


class ClearPoints extends React.Component{

  constructor(props) {
    super(props);
    this.state={
      loading:false,
      message:'-1',
      isSuccess:false,
      isShow:false,
    }
    this.userService =new UserService();
    this.clearPoints = this.clearPoints.bind(this);
    this.clearPointsCallBack = this.clearPointsCallBack.bind(this);

  }
  state={
    loading:false,
    message:'-1',
    isSuccess:false,
    isShow:false,
  }
  userService:UserService;

  clearPoints(e){
    let token = sessionStorage.getItem('adminLoginToken');
    this.userService.clearPoints(token,this.clearPointsCallBack);
    this.setState(
      {
        loading:true,
      }
    );

  }

  clearPointsCallBack(res,isSuccess){
    if(!isSuccess||res.code!='200'){
        message.info(res.message);
      this.setState({
        loading:false,
        isSuccess:false,
        isShow:true,
        message:res.message,
      });
    }else{
      this.setState({
        loading:false,
        isSuccess:true,
        isShow:true,
        message:res.data,
      });
    }
  }

  cancelPoints(e){

  }

  isSuccess(message,isSuccess,isShow){
    if(!isShow){
      return ;
    }else{
      if(isSuccess){
        let title= "成功更新"+eval(message)+"条数据";
        return <Alert message={title} type="success" showIcon />;
      }else{
        let title= "更新失败";
        return <Alert message={title} description={message} type="error" showIcon />
      }
    }

  }

  render() {
    const {loading,message,isSuccess,isShow} = this.state;
    return (
      <div className={styles.normal}>
        <div className={styles.normal_content}>
        <Alert
          message="此按钮为清零所有用户历史累计和当前可用积分，且不可回滚请慎重点击！"
          type="warning"
        />
        <div className={styles.normal_div}>
          <Popconfirm
            title="确定将所有人的积分清零?"
            onConfirm={this.clearPoints}
            onCancel={this.cancelPoints}
            okText="确认清零"
            cancelText="取消">
            <Button loading={loading} type="primary">清零</Button>
          </Popconfirm>
        </div>
        {this.isSuccess(message,isSuccess,isShow)}
        </div>
      </div>
    );
  }
}

export default ClearPoints;
